function WKTToGeometry() {

}

WKTToGeometry.write = function (wkt) {
    var geometry = null;
    var wkid=4490;
    var reg = /^\s*(\w+)\s*(\(+[^\(\)]+\)+)/
    var regValue = reg.exec(wkt);
    if (!regValue) {
        return null;
    }
    var wktType = regValue[1].toLowerCase();
    //var wktJson = regValue[2];
    var wktJson = wkt.toLowerCase().replace(wktType, "");
    switch (wktType) {
        case "point":
            var pointJson = wktJson.replace(/\(/g, "[").replace(/\)/g, "]");
            var re = /([-?\d\.]+[eE]?\d?)\s([-?\d\.]+[eE]?\d?)/g;
            pointJson = pointJson.replace(re, "[$1,$2]");
            pointJson = eval("(" + pointJson + ')');
            geometry = {
                type: "point",
                longitude:pointJson[0][0],
                latitude:pointJson[0][1],
                spatialReference:{wkid:wkid}
            };
            break;
        case "linestring":
            var lineJson = wktJson.replace(/\(/g, "[").replace(/\)/g, "]");
            var re = /([-?\d\.]+[eE]?\d?)\s([-?\d\.]+[eE]?\d?)/g;
            lineJson = lineJson.replace(re, "[$1,$2]");
            lineJson = Util.decode(lineJson);
            geometry = {
                type: "polyline",  // autocasts as new Polyline()
                paths:lineJson,
                spatialReference:{wkid:wkid}
            };
            break;
        case "multilinestring": //多段线
            var json = "[";
            wkt = wkt.substring(wkt.indexOf("(") + 1, wkt.lastIndexOf(")"));
            var lineJson = wkt.replace(/\(/g, "[").replace(/\)/g, "]");
            re = /([\d\.]+[eE]?\d?)\s([\d\.]+[eE]?\d?)/g;        // 创建正则表达式模式。
            lineJson = lineJson.replace(re, "[$1,$2]");
            json += lineJson;
            json += "]";
            json = Util.decode(json);
            geometry = {
                type: "polyline",  // autocasts as new Polyline()
                paths:json,
                spatialReference:{wkid:wkid}
            };
            break;
        case "polygon":
            var gonJson = wktJson.replace(/\(/g, "[").replace(/\)/g, "]");
            var re = /([-?\d\.]+[eE]?\d?)\s([-?\d\.]+[eE]?\d?)/g;
            gonJson = gonJson.replace(re, "[$1,$2]");
            gonJson = Util.decode(gonJson);
            geometry = {
                type: "polygon",
                hasZ: false,
                hasM: false,
                rings: gonJson,
                spatialReference:{wkid:wkid}
            };
            break;
        case "multipolygon":
            var json = "[";
            wkt = wkt.substring(wkt.indexOf("(") + 1, wkt.lastIndexOf(")"));
            var lineJson = wkt.replace(/(\({2})/g, "(");
            lineJson = lineJson.replace(/(\){2})/g, ")");
            var lineJson = lineJson.replace(/\(/g, "[").replace(/\)/g, "]");
            re = /([-?\d\.]+[eE]?\d?)\s([-?\d\.]+[eE]?\d?)/g;        // 创建正则表达式模式。
            lineJson = lineJson.replace(re, "[$1,$2]");
            json += lineJson;
            json += "]";
            json = Util.decode(json);
            geometry = {
                type: "polygon",
                hasZ: false,
                hasM: false,
                rings: json,
                spatialReference:{wkid:wkid}
            };
            break;
        default:
            geometry = null;
    }

    return geometry;
};
/**
 * WKTTOGeometry所需的工具类
 * @constructor
 */
function Util() {
}
/**
 * @Util.JSON 此处全部使用Ext.util.JSON,只是将相关对象添加到这里使用
 */
Util.JSON = new (function () {
    var useHasOwn = !!{}.hasOwnProperty,
        isNative = function () {
            var useNative = null;

            return function () {
                if (useNative === null) {
                    useNative = USE_NATIVE_JSON && window.JSON && JSON.toString() == '[object JSON]';
                }

                return useNative;
            };
        }(),
        USE_NATIVE_JSON = false,
        pad = function (n) {
            return n < 10 ? "0" + n : n;
        },
        doDecode = function (json) {
            return eval("(" + json + ')');
        },
        doEncode = function (o) {
            if (!isDefined(o) || o === null) {
                return "null";
            } else if (isArray(o)) {
                return encodeArray(o);
            } else if (isDate(o)) {
                return encodeDate(o);
            } else if (isString(o)) {
                return encodeString(o);
            } else if (typeof o == "number") {
                //don't use isNumber here, since finite checks happen inside isNumber
                //isFinite函数用于检查其参数是否是无穷大
                return isFinite(o) ? String(o) : "null";
            } else if (isBoolean(o)) {
                return String(o);
            } else {
                var a = ["{"], b, i, v;
                for (i in o) {
                    // don't encode DOM objects
                    if (!o.getElementsByTagName) {
                        if (!useHasOwn || o.hasOwnProperty(i)) {
                            v = o[i];
                            switch (typeof v) {
                                case "undefined":
                                case "function":
                                case "unknown":
                                    break;
                                default:
                                    if (b) {
                                        a.push(',');
                                    }
                                    a.push(doEncode(i), ":",
                                        v === null ? "null" : doEncode(v));
                                    b = true;
                            }
                        }
                    }
                }
                a.push("}");
                return a.join("");
            }
        },
        m = {
            "\b": '\\b',
            "\t": '\\t',
            "\n": '\\n',
            "\f": '\\f',
            "\r": '\\r',
            '"': '\\"',
            "\\": '\\\\'
        },
        encodeString = function (s) {
            if (/["\\\x00-\x1f]/.test(s)) {
                return '"' + s.replace(/([\x00-\x1f\\"])/g, function (a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return "\\u00" +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    }) + '"';
            }
            return '"' + s + '"';
        },
        encodeArray = function (o) {
            var a = ["["], b, i, l = o.length, v;
            for (i = 0; i < l; i += 1) {
                v = o[i];
                switch (typeof v) {
                    case "undefined":
                    case "function":
                    case "unknown":
                        break;
                    default:
                        if (b) {
                            a.push(',');
                        }
                        a.push(v === null ? "null" : Util.JSON.encode(v));
                        b = true;
                }
            }
            a.push("]");
            return a.join("");
        },
        encodeDate = function (o) {
            return '"' + o.getFullYear() + "-" +
                pad(o.getMonth() + 1) + "-" +
                pad(o.getDate()) + "T" +
                pad(o.getHours()) + ":" +
                pad(o.getMinutes()) + ":" +
                pad(o.getSeconds()) + '"';
        },
        isDate = function (v) {
            return Object.prototype.toString.apply(v) === '[object Date]';
        },
        isArray = function (v) {
            return Object.prototype.toString.apply(v) === '[object Array]';
        },
        isString = function (v) {
            return typeof v === 'string';
        },
        isBoolean = function (v) {
            return typeof v === 'boolean';
        },
        isDefined = function (v) {
            return typeof v !== 'undefined';
        };


    /**
     *

     Encodes a Date. This returns the actual string which is inserted into the JSON string as the literal expression.
     * The returned value includes enclosing double quotation marks.

     *

     The default return format is "yyyy-mm-ddThh:mm:ss".

     *

     To override this:


     Ext.util.JSON.encodeDate = function(d) {
    return d.format('"Y-m-d"');
    };

     /**
     * Encodes an Object, Array or other value
     * @param {Mixed} o The variable to encode
     * @return {String} The JSON string
     */
    this.encode = function () {
        var ec;
        return function (o) {
            if (!ec) {
                // setup encoding function on first access
                ec = isNative() ? JSON.stringify : doEncode;
            }
            return ec(o);
        };
    }();


    /**
     * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError unless the safe option is set.
     * @param {String} json The JSON string
     * @return {Object} The resulting object
     */
    this.decode = function () {
        var dc;
        return function (json) {
            if (!dc) {
                // setup decoding function on first access
                dc = isNative() ? JSON.parse : doDecode;
            }
            return dc(json);
        };
    }();

})();
/**
 * Shorthand for {@link Ext.util.JSON#encode}
 * @param {Mixed} o The variable to encode
 * @return {String} The JSON string
 * @member Ext
 * @method encode
 */
Util.encode = Util.JSON.encode;
/**
 * Shorthand for {@link Ext.util.JSON#decode}
 * @param {String} json The JSON string
 * @param {Boolean} safe (optional) Whether to return null or throw an exception if the JSON is invalid.
 * @return {Object} The resulting object
 * @member Ext
 * @method decode
 */
Util.decode = Util.JSON.decode;


Array.prototype.indexOf = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) {
            return i;
        }
    }
    return -1;
};
Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

